﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PokeData;

namespace PokeTD_MapEditor
{
    public partial class WaveEdit : Form
    {
        public WaveEdit(WavesDS myDS)
        {
            InitializeComponent();
            wds = myDS;
        }

        WavesDS wds;

        private string _fileName = "";

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private void WaveEdit_Load(object sender, EventArgs e)
        {
            if (wds.Waves.Rows.Count == 0)
                splitContainer1.Enabled = false;

            refreshList(0);
        }

        private void lbWaves_SelectedIndexChanged(object sender, EventArgs e)
        {
            var wr = from wv in wds.Waves
                     where wv.id == (int)(lbWaves.SelectedItem as ComboboxItem).Value
                     select wv;
            var cr = from ce in wds.Creeps
                    select ce;

            WavesDS.WavesRow w = wr.First<WavesDS.WavesRow>();

            txtName.Text = w.WaveName;

            cbCreeptyp.Items.Clear();
            int i = 0;
            foreach (WavesDS.CreepsRow c in cr)
            {
                cbCreeptyp.Items.Add(new ComboboxItem(c.CreepName, c.id));
                if (c.id == w.CreepType)
                    cbCreeptyp.SelectedIndex = i;
                i++;
            }

            nudAnzahl.Value = (decimal)w.CreepCount;
            nudSecToNext.Value = (decimal)w.SecondsToNextWave;
            nudSpawnTime.Value = (decimal)w.SpawnTime;
        }

        private void bttnNeu_Click(object sender, EventArgs e)
        {
            wds.Waves.AddWavesRow("Neue Wave", wds.Creeps[0].id, 1, 20, 0.1);
            refreshList(lbWaves.Items.Count);
        }

        private void refreshList(int sel)
        {
            lbWaves.Items.Clear();
            var wr = from wv in wds.Waves
                     select wv;

            foreach (WavesDS.WavesRow w in wr)
            {
                lbWaves.Items.Add(new ComboboxItem(w.WaveName, w.id));
            }
            lbWaves.SelectedIndex = sel;
        }

        private void bttnLoeschen_Click(object sender, EventArgs e)
        {
            var wr = from wv in wds.Waves
                     where wv.id == (int)(lbWaves.SelectedItem as ComboboxItem).Value
                     select wv;
            WavesDS.WavesRow w = wr.First<WavesDS.WavesRow>();

            w.Delete();
        }

        private void saveToDB(object sender, EventArgs e)
        {
            var wr = from wv in wds.Waves
                     where wv.id == (int)(lbWaves.SelectedItem as ComboboxItem).Value
                     select wv;
            WavesDS.WavesRow w = wr.First<WavesDS.WavesRow>();

            w.WaveName = txtName.Text;
            w.CreepType = (int)(cbCreeptyp.Items[cbCreeptyp.SelectedIndex] as ComboboxItem).Value;
            w.CreepCount = (int)nudAnzahl.Value;
            w.SecondsToNextWave = (int)nudSecToNext.Value;
            w.SpawnTime = (double)nudSpawnTime.Value;
            refreshList(lbWaves.SelectedIndex);
        }

    }
}
